#ifndef HANDLEREQUESTCHATROOM_H
#define HANDLEREQUESTCHATROOM_H

#include"ChattingRoom.h"
#include"MsgType.h"
#include<queue>
#include<string>
#include<memory>

using namespace std;

//管理邀请请求
class InviteHandler{
    struct Invitation{
        int from_id; //邀请人id
        int to_id; //被邀请人id
        string chat_room_id; //聊天室id
    };
    using Invitation_ptr=unique_ptr<Invitation>;
    static constexpr const char* INVITE_FILE_DIR = "data/invitation/";
    public:
    int delay_time; //邀请处理延迟时间    
    queue<Invitation_ptr> invitations; //邀请队列

    void add_invitation(const int& from_id,const int& to_id,const string& chat_room_id); //添加邀请

    void send_out_invitation(Invitation_ptr& invitation); //如果用户在线，则发送邀请

    void save_invitation(Invitation_ptr& invitation); //如果用户离线，则保存邀请

    void load_invitation(const int&to_id); //如果用户登录，则加载邀请到队列中

    string get_invitation_file_path(const int& to_id) const; //获得邀请文件路径

    int open_file(const string& file_path, ofstream& ofs) const; //打开文件
    int open_file(const string& file_path, ifstream& ifs) const; //打开文件
    
    void listen_invitation(); //监听邀请队列，处理邀请
};

//管理拒绝邀请请求
class InvitationResultHandlerManager{
    struct InvitationResult{
        int from_id; //邀请人id
        int to_id; //被邀请人id
        string chat_room_id; //聊天室id
        bool is_agree; //是否同意
    };

    public:
    using InvitationResult_ptr=unique_ptr<InvitationResult>;
    static constexpr const char* REFUSE_INVITATION_FILE_DIR = "data/refuse_invitation/";

    void add_refuse_invitation(const int& from_id,const int& to_id,const string& chat_room_id); //添加拒绝邀请

    void save_refuse_invitation(InvitationResult_ptr& refuse_invitation); //保存拒绝邀请

    void load_refuse_invitation(const int&to_id); //加载拒绝邀请

    string get_refuse_invitation_file_path(const int& to_id) const; //获得拒绝邀请文件路径

    int open_file(const string& file_path, ofstream& ofs) const; //打开文件
    int open_file(const string& file_path, ifstream& ifs) const; //打开文件
    void listen_invitation_result(); //监听邀请结果队列，处理邀请结果
};

//管理退出聊天室通知
class QuitChatRoomNotification{
      struct QuitChatRoom{
        int user_id; //用户id
        string chat_room_id; //聊天室id
        string time; //退出时间
        string reason; //退出原因
      };
      public:
      using QuitChatRoom_ptr=unique_ptr<QuitChatRoom>;
      static constexpr const char* QUIT_CHAT_ROOM_FILE_DIR = "data/quit_chat_room/";

      void add_quit_chat_room(const int& user_id,const string& chat_room_id,const string& reason,const string& time); //添加退出聊天室通知

      void save_quit_chat_room(QuitChatRoom_ptr& quit_chat_room); //保存退出聊天室通知

      void load_quit_chat_room(const int&user_id); //加载退出聊天室通知

      string get_quit_chat_room_file_path(const int& user_id) const; //获得退出聊天室通知文件路径

      int open_file(const string& file_path, ofstream& ofs) const; //打开文件
      int open_file(const string& file_path, ifstream& ifs) const; //打开文件
      void listen_quit_chat_room(); //监听退出聊天室通知队列，处理退出聊天室通知

};


class ChatRoomHandler{
    private:
        unique_ptr<InviteHandler> invite_handler;
        unique_ptr<InvitationResultHandlerManager> invitation_result_handler_manager;
        unique_ptr<QuitChatRoomNotification> quit_chat_room_notification;
        unique_ptr<ChattingRoomManager> chatting_room_manager;

    public:
        ChatRoomHandler()=default;
        ~ChatRoomHandler()=default;

};

#endif